import $ from "jquery"
const tools = {
  isMobile: function () {
    var userAgent = navigator.userAgent.toUpperCase().toString();
    var isMobile =
      (userAgent.indexOf("IOS") >= 0) ||
      (userAgent.indexOf("ANDROID") >= 0) ||
      (userAgent.indexOf("IPHONE") >= 0);
    return isMobile;
  },
  checkPhoneNum: function (sMobile) {
    if (!(/^1[0-9][0-9]\d{8}$/.test(sMobile))) {
      return false;
    } else {
      return true;
    }
  },
  getPageParameter: function (key) {
    if (!!!key) {
      this.errorMSg('getPageParameter can not accept invalid key !')
    }
    let urlStr = window.location.href;
    let parameterStr = urlStr.split("?")[1];
    let parameterStrArr = parameterStr.split("&");

    let filterItem = parameterStrArr.filter(function (item) {
      return item.indexOf("key") >= 0;
    });

    //		console.log(filterItem)
    if (filterItem.length) {
      let value = filterItem.splice("=")[1];
      return value;
    } else {
      return false;
    }

    //		console.log(parameterStrArr)
  },
  warnMsg: function (msg) {
    console.log("\n %cYONGHU-TIPS-WARNING:\n %s \n", "color:orange;", msg);
  },
  errorMSg: function (msg) {
    console.log("\n %cYONGHU-TIPS-ERROR:\n %s \n", "color:red;", msg);
  }
}

function registTouchAndclick(params) {
  if (tools.isMobile()) {
    $(document).on("touchstart", function (e) {
      $(e.target).data("isMoved", 0);
    });

    $(document).on("touchmove", function (e) {
      $(e.target).data("isMoved", 1);
    });

    $(document).on("touchend", function (e) {
      if ($(e.target).data("isMoved") == 0) {
        $(e.target).trigger("tap");
      }
    });
  } else {
    $(document).on("click", function (e) {
      $(e.target).trigger("tap");
    });
  };
}

const timer = setTimeout(() => {
  if (!window) {
    timer();
    return;
  }
  window.onload = () => {
    registTouchAndclick();
  }
}, 500);

export default tools
export { tools }